package yto.freemarker.springaop.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * 自定义的bean工厂的后置处理器
 *
 *
 * @author dongxing
 **/
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    /**
     * 在bean实例化（IOC）之前被调用，只是刚刚创建了BeanDefinition
     *
     * @param beanFactory
     * @throws BeansException
     */

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        System.out.println("执行工厂执行!!");
        //获取Bean对象
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("userService");
        //可以干预beanDefinition的值（将单例设置为多例）
        beanDefinition.setScope("prototype");
    }
}
